package com.yf.quartz.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.Utils.PageUtil;
import com.yf.quartz.service.api.QuartzJobService;
import com.yf.quartz.domain.QuartzJob;
import com.yf.quartz.domain.QuartzLog;
import com.yf.quartz.model.query.QuartzJobQueryCriteria;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Set;


/**
 * @author chentianwei
 * @date 2024-11-01 16:54
 * @description 定时任务Controller
 */
@RestController
@RequestMapping("/api/jobs")
@Slf4j
@Tag(name = "QuartzJobApi", description = "Quartz定时任务")
public class QuartzJobController {

    private final QuartzJobService quartzJobService;

    public QuartzJobController(QuartzJobService quartzJobService) {
        this.quartzJobService = quartzJobService;
    }

    @GetMapping("")
    @Operation(summary = "获取Job任务列表", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<Object>> queryQuartzJob(QuartzJobQueryCriteria criteria) {
        List<QuartzJob> quartzJobs = quartzJobService.queryAll(criteria);
        return ResponseEntity.ok(ResponseBody.success(PageUtil.toPage(quartzJobs, quartzJobs.size())));
    }

    @PostMapping("/add")
    @Operation(summary = "添加Job任务列表", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> addQuartzJob(@RequestBody QuartzJob quartzJob) {
        quartzJobService.create(quartzJob);
        return ResponseEntity.ok(ResponseBody.success("添加成功"));
    }

    @PutMapping("/edit")
    @Operation(summary = "修改Job任务列表", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> editQuartzJob(@RequestBody QuartzJob quartzJob) {
        quartzJobService.update(quartzJob);
        return ResponseEntity.ok(ResponseBody.success("修改成功"));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Job任务列表", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> deleteQuartzJob(@RequestBody Set<Long> ids) {
        quartzJobService.delete(ids);
        return ResponseEntity.ok(ResponseBody.success("删除成功"));
    }

    @GetMapping("/download")
    @Operation(summary = "下载Job任务列表", security = @SecurityRequirement(name = "authScheme"))
    public void downloadQuartzJob(QuartzJobQueryCriteria criteria, HttpServletResponse response) {
        try {
            quartzJobService.download(criteria, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PutMapping("/execute/{id}")
    @Operation(summary = "执行Job任务", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> executeQuartzJob(@PathVariable Long id) {
        quartzJobService.execution(quartzJobService.getByQuartzId(id));
        return ResponseEntity.ok(ResponseBody.success("开始执行~~~"));
    }

    @PutMapping("/{id}")
    @Operation(summary = "变更Job任务列表", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> changeJobStatus(@PathVariable Long id) {
        quartzJobService.updateIsPause(quartzJobService.getByQuartzId(id));
        return ResponseEntity.ok(ResponseBody.success("状态变更成功"));
    }

    @GetMapping(value = "/logs")
    @Operation(summary = "获取Job日志列表", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<Object>> queryQuartzJobLog(QuartzJobQueryCriteria criteria, Page<Object> page) {
        List<QuartzLog> quartzLogs = quartzJobService.queryAllLog(criteria);
        return ResponseEntity.ok().body(ResponseBody.success(PageUtil.toPage(quartzLogs, quartzLogs.size())));
    }

    @GetMapping(value = "/logs/download")
    @Operation(summary = "导出Job日志列表", security = @SecurityRequirement(name = "authScheme"))
    public void exportQuartzJobLog(HttpServletResponse response, QuartzJobQueryCriteria criteria) throws IOException {
        quartzJobService.downloadLog(quartzJobService.queryAllLog(criteria), response);
    }


}
